<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
// 
//  activity_model.php
//  www.6block.com
//  
//  Created by Will_Lee on 2011-05-17.
//  Copyright 2011 lishuzu. All rights reserved.
// 
class Admin_work_model extends CI_Model
{
		function __construct(){
			parent::__construct();
			$this->user = $this->session->userdata('user');
			$this->workstatus = array(
			 						'0'=>'未审核',
			 						'1'=>'审核通过',
			 						'2'=>'推荐',
									'3'=>'删除',
			);
		}
		
		function get_list(){
			
			$list = $multipage_config = $uids = array();			
			$mpurl = base_url().'admin/admin_work/index/?ac=admin_work';
			$pre = '';

			//处理搜索
			$intkeys = array('uid', 'status', 'wid');
			$strkeys = array('username');
			$randkeys = array(array('sstrtotime','dateline'), array('intval','viewnum'), array('intval','replynum'), array('intval','likenum'));
			$likekeys = array('title', 'description');
			$results = getwheres($intkeys, $strkeys, $randkeys, $likekeys, '');
			$wherearr = $results['wherearr'];
			$wheresql = empty($wherearr)?'1':implode(' AND ', $wherearr);
			$mpurl .= '&'.implode('&', $results['urls']);

			
			//激活
			$actives = array($_GET['tab'] => ' class="active"');
			if(!isset($_GET['tab'])) {
				$actives = array('all' => ' class="active"');
			} else {
				$mpurl .= '&tab='.$_GET['tab'];
			}
			
			//排序
			$orders = getorders(array('dateline', 'viewnum', 'likenum', 'replaynum'), 'wid', $pre);
			$ordersql = $orders['sql'];
			if($orders['urls']) $mpurl .= '&'.implode('&', $orders['urls']);
			$orderby = array($_GET['orderby']=>' selected');
			$ordersc = array($_GET['ordersc']=>' selected');
			
			//每页查看数
			$perpage = empty($_GET['perpage'])?0:intval($_GET['perpage']);
			if(!in_array($perpage, array(20,50,100))) $perpage = 20;
			$perpages = array($perpage => ' selected');
			
			//分页
			$multipage_config = $this->config->item('multipage_config', 'SGLOBAL');
			$multipage_config['base_url'] = $mpurl;
			$multipage_config['page_query_string'] = TRUE;
			$multipage_config['uri_segment'] = '4';
			$offset = $this->input->get('per_page') ? $this->input->get('per_page', true) : 0;
			$result = $this->db->query("SELECT COUNT(*) as num FROM ".$this->db->dbprefix('works')." WHERE $wheresql")->row_array();
			
			$count = $result['num'];
			
			if($count) {
				$query = $this->db->query("SELECT * FROM ".$this->db->dbprefix('works')." WHERE $wheresql $ordersql LIMIT $offset, ".$multipage_config['per_page']);
				foreach ($query->result_array() as $row) {
					$row['originimg'] = get_workimg($row['filepath'], 'origin', 'url');
					$row['bigimg'] = get_workimg($row['filepath'], 'big', 'url');
					$row['thumbimg'] = get_workimg($row['filepath'], 'thumb', 'url');
					//状态描述
					$row['workstatus'] = $this->workstatus[$row['status']] ? $this->workstatus[$row['status']] : '未知';
					$list[] = $row;
				}
				
				$multipage_config['per_page'] = $perpage;
				$multipage_config['total_rows'] = $count;
				$this->pagination->initialize($multipage_config);
				$multi = $this->pagination->create_links();
				$data['multi'] = $multi;
			}
			
			$data['actives'] = $actives;
			$data['perpages'] = $perpages;
			$data['orders'] = $orders;
			$data['orderby'] = $orderby;
			$data['list'] = $list;
			return $data;
		}
}
?>